package com.sywb.chuangyebao.contract;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.view.fragment.IncomeAndDependitureDetailsFragment;
import com.sywb.chuangyebao.widget.XTabLayout;

import org.bining.footstone.mvp.IView;
import org.bining.footstone.presenter.BasePresenter;

import java.util.ArrayList;
import java.util.List;

public interface IncomeDependitureDetailsContract {
    interface IncomeDependitureDetailsView extends IView{
        FragmentManager getUserFragmentManager();
    }

    class InconmeDependitureDetailsPresenter extends BasePresenter<IncomeDependitureDetailsView>{
         private ViewPager viewPager;
         private ChannelPagerFragmentAdapter pagerFragmentAdapter = null;
         private List<Fragment> fragmentList;
         private List<String> channels;
        @Override
        public void onStart() {
            fragmentList = new ArrayList<>();
            channels = new ArrayList<>();
        }



        /**
         * 初始化View
         * @param viewPager
         * @param xTabLayout
         */
        public void initTabView(ViewPager viewPager, XTabLayout xTabLayout){
            this.viewPager = viewPager;
            initThreeView(mContext.getResources().getStringArray(R.array.income_title));
            viewPager.setCurrentItem(0);
            xTabLayout.setupWithViewPager(viewPager);
        }


        private void initThreeView(String[] titles){
            for (int i=0;i<titles.length;i++){
                Fragment fragment = IncomeAndDependitureDetailsFragment.newInstance(i);
                channels.add(titles[i]);
                fragmentList.add(fragment);
            }
            pagerFragmentAdapter = new ChannelPagerFragmentAdapter(mView.getUserFragmentManager(),fragmentList,channels);
            viewPager.setAdapter(pagerFragmentAdapter);
            viewPager.setOffscreenPageLimit(fragmentList.size());
            viewPager.setCurrentItem(0);
        }



        class ChannelPagerFragmentAdapter extends FragmentStatePagerAdapter {

            private List<Fragment> fragments;
            private List<String> channels;

            public ChannelPagerFragmentAdapter(FragmentManager fragmentManager, List<Fragment> fragments, List<String> channels) {
                super(fragmentManager);
                this.fragments = fragments;
                this.channels = channels;
            }


            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return channels.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return channels == null ? "" : channels.get(position);
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        }
    }
}
